<?php
/**
*名称:ajax_2.php
*作用:二期工程ajax
*说明:
*版权:
*作者:Red	QQ:316765128
*时间:2011/10/28
**/
include_once './include/init.inc.php';

include_once './include/cpc_qplus.cls.php';
include_once './include/model.cls.php';
$model = new model_sql();

$user_id = getCookie('user_id');

if(empty($s)){
	exit();
}
$re = '';
if($s == 'qq_login'){	//登录
	$qq->redirect_to_login($Global['qq']['callback']);
	exit();
}elseif($s=='cropImage'){	//保存头像	$filename,$x1,$y1,$x2,$y2
	try{
		$file_dir = ROOT.'upload/avatar/';
		$tname = $filename;
		$filename = $file_dir.$tname;
		$newName = $file_dir.getImgName($tname,'0');
		list($oldWidth, $oldHeight, $type) = getimagesize($filename);	//取得图像大小
		$ext = image_type_to_extension($type);	//取得图像类型的文件后缀
		//根据扩展名，用相应的方法打开原文件。
		switch($ext){
			case '.gif' :
				$oldImage = imagecreatefromgif($filename);
				break;
			case '.png' :
				$oldImage = imagecreatefrompng($filename);
				break;
			case '.jpg' :
			case '.jpeg' :
				$oldImage = imagecreatefromjpeg($filename);
				break;
			default :
				echo "不支持此文件格式！";
				exit();
		}
		//create new image
		$w=($x2-$x1);
		$h=($y2-$y1);
		$newImage = imagecreatetruecolor($w, $h);
		//put old image on top of new image
		imagecopyresampled($newImage, $oldImage, 0, 0, $x1,$y1,$w,$h,$w,$h);
		$quality=75;
		switch($ext){
			case '.gif' :
				imagegif($newImage, $newName);
				break;
			case '.png' :
				imagepng($newImage, $newName);
				break;
			case '.jpg' :
			case '.jpeg' :
				imagejpeg($newImage, $newName, $quality);
				break;
			default :
				echo "不支持此文件格式！";
				exit();
		}
		imagedestroy($newImage);
		$newName0 = getImgName($tname,'0');
		$newName1 = getImgName($tname,'1');
		$newName2 = getImgName($tname,'2');
		$newName3 = getImgName($tname,'3');
		$PImage = new pImageFile();
		$PImage->pimage('resizeCrop', $newName0, $file_dir, $newName1, 178, 178);
		$PImage->pimage('resizeCrop', $newName0, $file_dir, $newName2, 118, 118);
		$PImage->pimage('resizeCrop', $newName0, $file_dir, $newName3, 60, 60);
		if(file_exists($newName)){unlink($newName);}
		if(file_exists($filename)){unlink($filename);}
		//更新用户头像数据
		$db -> query("update `info_link_user` set `photo`='$tname' where `id`='$user_id'");
		echo '1';
	}catch(Exception $e){
		exit('0');
	}
}elseif($s=='add_info'){	//更新用户信息
	$nick = contentReplace($nick);
	$name = contentReplace($name);
	if($province == '==请选择省/直辖市==' or empty($province) or $province ==' '){
		$province = '';
	}else{
		$province = contentReplace($province);
	}
	if($city == '==城市/地区==' or $city == 'null' or empty($city)){
		$city = '';
	}else{
		$city = contentReplace($city);
	}
	$gender = contentReplace($gender);
	$birthyear = contentReplace($birthyear);
	$birthmonth = contentReplace($birthmonth);
	$birthday = contentReplace($birthday);
	$qq = contentReplace($qq);

	$db -> query("update `info_link_user` set `nick`='$nick',`name`='$name',`province`='$province',`city`='$city',`gender`='$gender',`birthyear`='$birthyear',`birthmonth`='$birthmonth',`birthday`='$birthday',`qq`='$qq' where `id`='$user_id'");
	exit('1');
}elseif($s == 'followUser'){	//关注某人
	//$user_id 关注者id
	if(!is_numeric($id) or empty($user_id)){	//被关注人id
		exit('0');
	}
	$re = $db -> fetch_one("select id from `info_q_follow` where `user_id`='$id' and `follow_id`='$user_id'");
	if(empty($re)){
		$dateline = time();
		$db -> query("insert into `info_q_follow`(`user_id`,`follow_id`,`dateline`) values('$id','$user_id','$dateline')");
	}
	exit('1');
}elseif($s == 'cancelFollow'){	//取消关注
	//$user_id 关注者id
	if(!is_numeric($id) or empty($user_id)){	//被关注人id
		exit('0');
	}
	$db -> query("delete from `info_q_follow` where `user_id`='$id' and `follow_id`='$user_id'");
	exit('1');
}elseif($s == 'likePet'){	//喜欢某人的宠物
	if(!is_numeric($pet_id) or empty($user_id)){
		exit('0');
	}
	$re = $db -> fetch_one("select id from `info_pet_qlike` where `user_id`='$user_id' and `pet_id`='$pet_id'");
	if(empty($re)){
		$dateline = time();
		$db -> query("insert into `info_pet_qlike`(`user_id`,`pet_id`,`dateline`) values('$user_id','$pet_id','$dateline')");
	}
	exit('1');
}elseif($s == 'noticeOld'){	//通知已读
	if(!is_numeric($id) or empty($user_id)){
		exit('0');
	}
	$db -> query("update `info_q_noticeIds` set `state`='1' where id='$id'");
	exit('1');
}elseif($s == 'checkFriendName'){	//检查好友昵称是否存在,存在返回好友ID
	if(empty($name) or empty($user_id)){
		exit('0');
	}
	//获取用户好友昵称
	$user_id_list = $model->getFriendIdList($user_id,2,0,20000);
	$friends = array();
	foreach($user_id_list as $key=>$val){
		$u = $model->getUserInfo($val['user_id']);
		if($u['nick']==$name){
			$friends[] = array('id'=>$u['id'],'avatar3'=>$u['avatar3']);
		}
	}
	if(empty($friends)){
		exit('0');
	}else{
		$re = json_encode($friends);
		exit($re);
	}
	/*	多用户发送
	//$name = str_replace("；", ";", $name);
	//$names = explode(";",$name);
	foreach($names as $key=>$val){
		if(!in_array($val,$nicks) and !empty($val)){
			exit('0');	//不存在此好友
		}
	}
	exit('1');*/
}elseif($s == "sendPM"){	//发送私信
	if(!empty($get_user_id) && !empty($content) && !empty($user_id)){
		$dateline = time();
		$content = contentReplace($content);
		$get_user_id = contentReplace($get_user_id);
		if($get_user_id<$user_id){
			$s_uid = $get_user_id;
			$b_uid = $user_id;
		}else{
			$s_uid = $user_id;
			$b_uid = $get_user_id;
		}
		$sql = "insert into `info_q_message`(`small_user_id`,`big_user_id`,`send_user_id`,`content`,`dateline`) values('".$s_uid."','".$b_uid."','".$user_id."','".$content."','".$dateline."')";	//插入私信
		$db->query($sql);

		/*	多用户发送
		$name = str_replace("；", ";", $name);
		$names = explode(";",$name);
		$user_id_list = $model->getFriendIdList($user_id,2,0,20000);	//获取用户好友昵称
		$nicks = $ids = array();
		foreach($user_id_list as $key=>$val){
			$u = $model->getUserInfo($val['user_id']);
			if(in_array($u['nick'],$names)){
				$ids[] = $u['id'];
			}
		}
		$sql = "insert into `info_q_message`(`send_user_id`,`get_user_id`,`content`,`dateline`) values";	//插入私信
		foreach($ids as $key=>$val){
			$sql .= "('".$user_id."','".$val."','".$dateline."'),";
		}
		$sql = substr($sql, 0,-1);
		$db->query($sql);
		*/
		exit('1');
	}
	exit('0');
}elseif($s == "searchGoodsName"){
	require_once ROOT.'include/sphinxapi.class.php';
	function goodsSearch($keyword,$limit=40){
		$cl = new SphinxClient();
		$cl->SetServer('121.52.221.126',3312);
		$cl->SetMatchMode(SPH_MATCH_ALL);		//设定搜索模式(完整匹配)
		$cl->SetRankingMode(SPH_RANK_PROXIMITY_BM25);	//同时使用词组评分和BM25评分
		$cl->SetArrayResult(true);
		$cl->SetLimits(0,(int)$limit);
		$cl->SetMaxQueryTime(5000);		//最多5秒
		$res = $cl->Query($keyword,'goods');
		#echo	$cl->GetLastError();
		#echo	$cl->GetLastWarning();
		$ids = '';
		if(!empty($res['matches'])){
			$i=0;
			$ids = '';
			foreach($res['matches'] as $key=>$val){
				empty($i) ? $ids .= $val['id'] : $ids .= ','.$val['id'];
				++$i;
				//$arr_ids[] = $val['id']; //排序用
			}
		}
		Return $ids;
	}
	if(!empty($a)){
		$ids = goodsSearch($a);	//搜索引擎
		//echo "$ids";
		if(!empty($ids)){
			$sql = "select goods_name from info_goods where goods_id in($ids)";
			$re = $db -> fetch_all($sql);
			foreach($re as $key=>$val){
				echo $val['goods_name']."<br>";
			}
		}else{
			echo "没有搜索到";
		}
		exit();
	}
}




